﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using Clipboard = System.Windows.Clipboard;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

namespace UploadClient.Utilitys
{
    /// <summary>
    /// Methods for capturing and save screenshots.
    /// </summary>
    public static class ImageHelpers
    {
        /// <summary>
        /// Saves a picture of the screen, captured in the specified image format, to a file.
        /// </summary>
        /// <param name="filename">The filepath and name of the image.</param>
        /// <param name="format">Specifi a format.</param>
        public static void SaveScreenShot(string filename, ImageFormat format)
        {
            Bitmap screenShot = CaptureScreenShot();
            screenShot.Save(filename, format);
        }

        /// <summary>
        /// Saves a picture of the screen to a bitmap image.
        /// </summary>
        /// <returns>The saved bitmap.</returns>
        public static Bitmap CaptureScreenShot()
        {
            // get the bounding area of the screen containing (0,0)
            // remember in a multidisplay environment you don't know which display holds this point
            Rectangle bounds = Screen.GetBounds(Point.Empty);

            // create the bitmap to copy the screen shot to
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);

            // now copy the screen image to the graphics device from the bitmap
            using (Graphics gr = Graphics.FromImage(bitmap))
            {
                gr.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }

            return bitmap;
        }

        /// <summary>
        /// Saves a picture of the defined screen to a bitmap image.
        /// </summary>
        /// <param name="src">UpperLeftSource</param>
        /// <param name="size">The picture size.</param>
        /// <returns>The saved bitmap.</returns>
        public static Bitmap CaptureScreenShot(Point src, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(src, new Point(0, 0), size);
            }

            return bitmap;
        }

        /// <summary>
        /// Save Image from clipboard to a png file.
        /// </summary>
        /// <param name="filePath">The path with the filename.</param>
        public static void SaveClipboardImageToFile(string filePath)
        {
            var image = Clipboard.GetImage();
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(fileStream);
            }
        }

        [DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);

        public static BitmapSource GetBitmapSource(Bitmap source)
        {
            IntPtr ip = source.GetHbitmap();
            BitmapSource bs;
            try
            {
                bs = Imaging.CreateBitmapSourceFromHBitmap(ip,
                IntPtr.Zero, Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                DeleteObject(ip);
            }

            return bs;
        }

    }
}
